﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FortuneHeaven.Entity.NetBar;
using Jzy.Core.Utility;
using FortuneHeaven.BLL.NetBar;
using Jzy.Core.Common.Basic;
using Jzy.Common;
using Jzy.Core.Admin;

namespace FortuneHeaven.Admin.Pages.NetBar
{
    public partial class UserEdit : PageEdit
    {
        private OperationMode Operation
        {
            get
            {
                return (OperationMode)int.Parse(StrUtil.DecryptDES(Request.QueryString["OperationMode"]));
            }
        }

        private UserExBO UserEx
        {
            get
            {
                return UserExBO.Instance<UserExBO>();
            }
        }

      

        private NetBarBO NetBar
        {
            get
            {
                return NetBarBO.Instance<NetBarBO>();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Operation == OperationMode.AddBoss)
                { 
                    drpNetBar.Visible = true;
                    litNetBar.Visible = false;

                    List<NetBarInfo> list = NetBar.GetList();
                    drpNetBar.DataSource = list;
                    drpNetBar.DataValueField = "ID";
                    drpNetBar.DataTextField = "NetbarName";
                    drpNetBar.DataBind();
                }
                else
                {
                    drpNetBar.Visible = false;
                    litNetBar.Visible = true;

                    UserExInfo info = UserEx.GetInfo(AppSession.UserID);
                    litNetBar.Text = info.NetBarName;
                }

                if (!string.IsNullOrEmpty(this.PKID))
                {
                    DispInfo();
                }
            }
        }

        private void DispInfo()
        {
            UserExInfo info = UserEx.GetInfo(this.PKID);
            this.LoginName.Text = info.UserInfo.LoginName;
            this.Name.Text = info.UserInfo.Name;
            txtPassword.Attributes.Add("value", StrUtil.EncryptMD5(info.UserInfo.Password));
            hidPassword.Value = StrUtil.EncryptMD5(info.UserInfo.Password);
            if (Operation == OperationMode.AddBoss)
            {
                drpNetBar.SelectedValue = info.NetBarID.ToString();
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            UserBO user = new UserBO();
            string strLoginName = LoginName.Text.Trim();
            bool blnFlag = user.ExistField(TableField.LoginName, strLoginName, this.PKID);

            if (blnFlag)
            {
                JSUtil.Alert("该登录名已存在");
            }
            else
            {
                UserInfo info = user.GetInfo((object)this.PKID);
                info.LoginName = this.LoginName.Text.Trim();
                info.Name = this.Name.Text.Trim();
                info.Orders = 0;                
              
                if (this.PKID == null)
                {
                    info.Status = (int)UserStatus.启用;
                }

                if (this.PKID == null)//新增
                {
                    info.Password = txtPassword.Text;
                    DeptBO dept = new DeptBO();
                    info.DeptIDs = new List<int>() { dept.GetRootID() };
                }
                else
                {
                    if (txtPassword.Text.Trim() != hidPassword.Value)
                    {
                        info.Password = txtPassword.Text;
                    }
                }
                user.Save(info);

                UserExInfo userExInfo = UserEx.GetInfo(this.PKID);
                userExInfo.ID = info.ID;
                if (Operation == OperationMode.AddBoss)
                {
                    userExInfo.NetBarID = int.Parse(drpNetBar.SelectedValue);
                    userExInfo.NetBarName = drpNetBar.SelectedItem.Text;
                }
                else
                {
                    UserExInfo bossInfo = UserEx.GetInfo(AppSession.UserID);
                    userExInfo.NetBarID = bossInfo.NetBarID;
                    userExInfo.NetBarName = bossInfo.NetBarName;                        
                }

                if (this.PKID == null)
                {
                    userExInfo.InputTime = DateTime.Now;
                    UserEx.Save(userExInfo, true);
                }
                else
                {
                    UserEx.Save(userExInfo,false);
                }
                

                JSUtil.RefreshView();
                
            }
        }
    }
}